'use strict';

/**
 * @ngdoc function
 * @name jgappApp.controller:UserdefinedUserdefinedCtrl
 * @description
 * # UserdefinedUserdefinedCtrl
 * Controller of the jgappApp
 */
angular.module('jgappApp')
    .controller('UserdefinedUserdefinedCtrl', function($scope, localStorageService, $rootScope, DataService, openApi, Util, $location, $routeParams, FormSubmit) {
        var id = $scope.process_id = $routeParams.id
        $scope.showtype = function() {
            var param = {};
            param.type = "自定义流程类型";
            Util.getKeyValue(param, function(data) {
                $scope.userTypeList = data.data
            });
        }
        $scope.choosepeople = function () {
            localStorageService.set("chooseUsrs", $scope.param.aduitUserIds);
            Util.savaParamForReview($scope.param, id);
            // 里面包含了title
            $location.path("userdefined/choosePerson/")
            // $location.path("chooseuser/index/getPageListByAccount");
        }
        $scope.getusers = function () {
            // type = [自定义流程审批人,通用合同审批人,借物资审批人,还物资审批人] 根据流程不同写死
            // parent = 用户选择类型的值
            var param = {
                type: "通用合同审批人",
                parent: $scope.param.field1
            }
            DataService.getData(openApi.getlistkeyvalues, param).then(function (data) {
                if (data) {
                    var user = data[0];
                    $scope.param.aduitUserNames = user && user['key'];
                    $scope.param.aduitUserIds = user && user['value'];
                }
            })
        }; 
        $scope.submit = function(operationId) {
            // 底部类型end
            var options = {};
            // 流程id
            options.id = id;
            // 根据operationId判断是草稿还是提交
            options.operationId = operationId;
            options.urlParam = openApi.caduitObject;
            options.param = angular.copy($scope.param, {});
            delete options.param.users
            FormSubmit.submit(options,$scope);
        };
        $scope.validate = function() {
            if (!$scope.param.field1) {
                Util.Toast("请填选择类型");
                return;
            }
            if (!$scope.param.title) {
                Util.Toast("请填写标题");
                return;
            }
            if (!$scope.param.content) {
                Util.Toast("请写事由");
                return;
            }
            if ($scope.param.relatedModuleName && $scope.param.relatedModuleName !== "无") {
                if (!$scope.param.moduleNo) {
                    Util.Toast("请选择模块编号");
                    return;
                }
            }
            if (!$scope.validation.passvalidation) {
                Util.Toast($scope.validation.tip);
                return;
            }
            return true;
        }

        $scope.delpersons = function(index) {
            $scope.param.users.splice(index, 1);
        };
        var init = function() {
            // 获取回显的详细信息
            Util.getDetailData(id, function(data) {
                $scope.param = data;
                var chooseUsrs = localStorageService.get("chooseUsrs");
                localStorageService.remove("chooseUsrs");
                // 获取选择页面返回的数据并且合并到参数中
                if(chooseUsrs){
                    $scope.param.aduitUserNames =chooseUsrs.aduitUserNames||"";
                    $scope.param.aduitUserIds = chooseUsrs.aduitUserIds||"";    
                }
            });
            $scope.showtype();
        };
        // 初始化
        init();
    });